﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TN_CMS.Admin.Helper.Common;
using TN_CMS.Admin.Models;
using TN_CMS.Domain.Abstract;
using TN_CMS.Domain.Entities;

namespace TN_CMS.Admin.Controllers
{
    public class StaticContentController : TNController
    {
        private readonly IStaticContentGroupRepository _staticContentGroupRepository;
        private readonly IStaticContentRepository _staticContentRepository;

        public StaticContentController(IStaticContentRepository _staticContentRepository, IStaticContentGroupRepository staticContentGroupRepository)
        {
            this._staticContentGroupRepository = staticContentGroupRepository;
            this._staticContentRepository = _staticContentRepository;
        }

        #region Static Content

        public ActionResult Index()
        {
            var model = new StaticContent();

            ViewBag.StaticContentGroups = new SelectList(this._staticContentGroupRepository.StaticContentGroups
                .Where(g => g.IsActive == true).ToList(), "Id", "Name");

            return View(model);
        }

        [HttpPost]
        public ActionResult StaticContent(int[] arrId)
        {
            var ckText = Request.Form["editor1"];
            var result = Json(new { message = "Success", data = ckText });

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetListStaticContent(int pageIndex, string orderClause)
        {
            var result = new CustomJsonResult();
            int total = 0;
            int pageSize = AdminHelper.GetPageSize();

            try
            {
                string whereClause = string.Empty;

                var lst = this._staticContentRepository.GetList(pageIndex, pageSize, out total, orderClause, whereClause);
                result.Result = lst;
                result.Optional = total;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetDetailStaticContent(int id)
        {
            var result = new CustomJsonResult();

            try
            {
                var detail = new StaticContent();

                if (id != -1)
                {
                    detail = this._staticContentRepository.GetDetail(id);
                }
                else
                {
                    detail.Id = -1;
                    detail.CreatedOn = DateTime.Now;
                    detail.IsActive = true;
                }

                result.Result = new
                {
                    Id = detail.Id,
                    Key = detail.Key,
                    Keywords = detail.Keywords,
                    Title = detail.Title,
                    Summary = detail.Summary,
                    Content = detail.Content,
                    StaticContentGroupId = detail.StaticContentGroupId,
                    TotalViews = detail.TotalViews,
                    CreatedOn = detail.CreatedOn,
                    CreatedBy = detail.CreatedBy,
                    UpdatedOn = detail.UpdatedOn,
                    UpdatedBy = detail.UpdatedBy,
                    IsActive = detail.IsActive,
                };
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult ManageStaticContent(StaticContent detail)
        {
            var result = new CustomJsonResult();

            try
            {
                this._staticContentRepository.Save(detail, 1);
                
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult UpdateActiveStaticContent(int[] arrId, bool isActive)
        {
            var result = new CustomJsonResult();

            try
            {
                this._staticContentRepository.UpdateActive(arrId, isActive);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteStaticContent(int[] arrId)
        {
            var result = new CustomJsonResult();

            try
            {
                this._staticContentRepository.Delete(arrId);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        #endregion

        #region Static content group

        public ActionResult Group()
        {
            StaticContentGroup model = new StaticContentGroup();
            return View(model);
        }

        [HttpPost]
        public ActionResult Group(int[] arrId)
        {
            var ckText = Request.Form["editor1"];
            var result = Json(new { message = "Success", data = ckText });

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetListStaticContentGroup(int pageIndex, string orderClause)
        {
            var result = new CustomJsonResult();
            int total = 0;
            int pageSize = AdminHelper.GetPageSize();

            try
            {
                string whereClause = string.Empty;

                var lst = this._staticContentGroupRepository.GetList(pageIndex, pageSize, out total, orderClause, whereClause);
                result.Result = lst;
                result.Optional = total;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetDetailStaticContentGroup(int id)
        {
            var result = new CustomJsonResult();

            try
            {
                var detail = new StaticContentGroup();

                if (id != -1)
                {
                    detail = this._staticContentGroupRepository.GetDetail(id);
                }
                else
                {
                    detail.Id = -1;
                    detail.CreatedOn = DateTime.Now;
                    detail.IsActive = true;
                }

                result.Result = new
                {
                    Id = detail.Id,
                    Name = detail.Name,
                    Description = detail.Description,
                    TotalViews = detail.TotalViews,
                    CreatedOn = detail.CreatedOn,
                    CreatedBy = detail.CreatedBy,
                    UpdatedOn = detail.UpdatedOn,
                    UpdatedBy = detail.UpdatedBy,
                    IsActive = detail.IsActive,
                };
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult ManageStaticContentGroup(StaticContentGroup detail)
        {
            var result = new CustomJsonResult();

            try
            {
                this._staticContentGroupRepository.Save(detail, 1);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult UpdateActiveStaticContentGroup(int[] arrId, bool isActive)
        {
            var result = new CustomJsonResult();

            try
            {
                this._staticContentGroupRepository.UpdateActive(arrId, isActive);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteStaticContentGroup(int[] arrId)
        {
            var result = new CustomJsonResult();

            try
            {
                this._staticContentGroupRepository.Delete(arrId);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        //public ActionResult UploadImage(string folder)
        //{
        //    string image = MediaHelper.GetImage("test", "image.jpg");
        //    // Check if the request contains multipart/form-data.
        //}

    #endregion
    }
}
